package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.dao.impl.logic.ResultSetParseEntityManger;
import com.yanqu.road.entity.tradewar.TradeWarWanted;

import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TradeWarWantedDaoImpl extends TempDataObjectDao<TradeWarWanted> {

	public Map<String, TradeWarWanted> getTradeWarWantedMap() {
		String sql = "select * from t_u_trade_war_wanted";
		ResultSet rs = null;
		CallableStatement call = null;
		Map<String, TradeWarWanted> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				TradeWarWanted data = parse(rs);
				dataMap.put(data.getWantedUid(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(TradeWarWanted data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_trade_war_wanted(wantedUid,userId,userBaseInfo,wantedUserId,wantedBaseInfo,wantedScore,wantedTime,battleTimes,"
				+ "reward,finishTime,scoreReward,useItemId) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getWantedUid());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getUserBaseInfoJsonStr());
			call.setLong(i++, data.getWantedUserId());
			call.setString(i++, data.getWantedBaseInfoJsonStr());
			call.setLong(i++, data.getWantedScore());
			call.setLong(i++, data.getWantedTime());
			call.setInt(i++, data.getBattleTimes());
			call.setString(i++, data.getReward());
			call.setLong(i++, data.getFinishTime());
			call.setBoolean(i++, data.isScoreReward());
			call.setInt(i++, data.getUseItemId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(TradeWarWanted data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_trade_war_wanted SET userId=?,userBaseInfo=?,wantedUserId=?,wantedBaseInfo=?,wantedScore=?,wantedTime=?,"
				+ "battleTimes=?,reward=?,finishTime=?,scoreReward=?,useItemId=? WHERE wantedUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getUserBaseInfoJsonStr());
			call.setLong(i++, data.getWantedUserId());
			call.setString(i++, data.getWantedBaseInfoJsonStr());
			call.setLong(i++, data.getWantedScore());
			call.setLong(i++, data.getWantedTime());
			call.setInt(i++, data.getBattleTimes());
			call.setString(i++, data.getReward());
			call.setLong(i++, data.getFinishTime());
			call.setBoolean(i++, data.isScoreReward());
			call.setInt(i++, data.getUseItemId());
			call.setString(i++, data.getWantedUid());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected TradeWarWanted parseData(ResultSet rs) throws SQLException {
		TradeWarWanted data = ResultSetParseEntityManger.initTradeWarWanted(rs);
		return data;
	}
}
